package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.PartGroupItem;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.PartGroupItemDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.PartGroupItemEntity;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class PartGroupItemDataHandler extends PartRelateDataHandler<PartGroupItem>{
	
	@Autowired
	private PartGroupItemDao partGroupItemDao;
	
	@Autowired
	private PartDataHandler partHandler;
	
	@Override
	public ResultBean save(PartGroupItem obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		partGroupItemDao.save(obj.getInnerData());
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功！");
		return resultBean;
	}
	
	@Override
	public ResultBean update(PartGroupItem obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		partGroupItemDao.update(obj.getInnerData());
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功！");
		return resultBean;
	}
	@Override
	public ResultBean delete(PartGroupItem obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);		
		partGroupItemDao.delete(obj.getInnerData());
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

	public PartGroupItem getById(long id) {
		PartGroupItem partGroupItem = new PartGroupItem();
		PartGroupItemEntity partGroupItemEntity = partGroupItemDao.getById(id);
		if (partGroupItemEntity != null) {
			partGroupItem.setInnerData(partGroupItemEntity);
		}
		return partGroupItem;
	}

	public PartGroupItem getByName(String name) {
		PartGroupItem partGroupItem = new PartGroupItem();
		PartGroupItemEntity partGroupItemEntity = partGroupItemDao.getByName(name);
		if (partGroupItemEntity != null) {
			partGroupItem.setInnerData(partGroupItemEntity);
		}
		return partGroupItem;
	}
	
	public List<PartGroupItem> getByPartGroupId(long partGroupId) {
		List<PartGroupItem> itemList = new ArrayList<PartGroupItem>();
		List<PartGroupItemEntity> entityList = partGroupItemDao.getByPartGroupId(partGroupId);
		if (entityList != null && entityList.size() > 0) {
			for (PartGroupItemEntity entity:entityList) {
				PartGroupItem partGroupItem = new PartGroupItem();
				partGroupItem.setInnerData(entity);
				Part part = partHandler.getById(entity.getPartId());
				partGroupItem.setPart(part);
				itemList.add(partGroupItem);
			}
			return itemList;
		}
		return null;
	}

	public void deleteByPartGroupId(PartGroupItem obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//	    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		partGroupItemDao.deleteByPartGroupId(obj.getInnerData());
	}

	@Override
	public ResultBean saveBatch(List<PartGroupItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<PartGroupItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<PartGroupItem> groupItemList) {
		ResultBean resultBean = new ResultBean(true, ' ');
		List<PartGroupItemEntity> entityList = new ArrayList<PartGroupItemEntity>();
			for (PartGroupItem groupItem:groupItemList) {
				PartGroupItemEntity partGroupItemEntity = groupItem.getInnerData();
				partGroupItemEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				partGroupItemEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
				partGroupItemEntity.setLastModifier(SpringWebUtils.getRequestUser());
//			    partGroupItemEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
				partGroupItemEntity.setRowTrxId(1);
				entityList.add(partGroupItemEntity);
			}
			partGroupItemDao.deleteBatch(entityList);
			resultBean.setSuccess(true);
			resultBean.setMessage("删除成功！");
			return resultBean;
	}

}
